package org.afs.gescol.core.dao.implementations;

import java.util.List;

import org.afs.gescol.core.dao.interfaces.SubjectManagmentDao;
import org.afs.gescol.core.dto.DataCriteria;
import org.afs.gescol.core.model.data.SubjectAssignment;
import org.afs.gescol.core.model.references.Subject;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.scub.foundation.framework.core.dao.implementations.HibernateDaoBaseImplementation;

/**
 * SubjectAssignmentDao implementation.
 * @autor Kobla GBENYO (kobla.gbenyo@gmail.com)
 */
public class SubjectManagmentDaoImpl extends HibernateDaoBaseImplementation implements SubjectManagmentDao {

    @Override
    public Subject getSubjectById(Long id) {
        return load(id, Subject.class);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Subject> getSubjectsByLabel(String label) {
        final Criteria criteria = createCriteria(Subject.class);
        if (label != null && !"".equals(label)) {
            criteria.add(Restrictions.or(
                Restrictions.ilike("shortLabel", label, MatchMode.ANYWHERE),
                Restrictions.ilike("longLabel", label, MatchMode.ANYWHERE)
            ));
        }
        criteria.addOrder(Order.asc("shortLabel"));
        return criteria.list();
    }

    @Override
    public void createSubjectAssignment(SubjectAssignment subjectAssignment) {
        save(subjectAssignment);
    }

    @Override
    public SubjectAssignment getSubjectAssignmentById(Long id) {
        return load(id, SubjectAssignment.class);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<SubjectAssignment> getSubjectAssignmentByLabel(String label) {
        final Criteria criteria = createCriteria(SubjectAssignment.class);
        if (label != null && !"".equals(label)) {
            criteria.add(Restrictions.or(
                Restrictions.or(
                    Restrictions.ilike("overrideShortLabel", label, MatchMode.ANYWHERE),
                    Restrictions.ilike("overrideLongLabel", label, MatchMode.ANYWHERE)
                ),
                Restrictions.or(
                    Restrictions.ilike("assignedSubject.shortLabel", label, MatchMode.ANYWHERE),
                    Restrictions.ilike("assignedSubject.longLabel", label, MatchMode.ANYWHERE)
                )
            ));
        }
        criteria.addOrder(Order.asc("overrideShortLabel")).addOrder(Order.asc("assignedSubject.shortLabel"));
        return criteria.list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<SubjectAssignment> getSubjectAssignmentByCriteria(DataCriteria criteria) {
        final Criteria searchCriteria = createCriteria(SubjectAssignment.class);
        if (criteria.getSeriesId() != null) {
            searchCriteria.add(Restrictions.eq("assignedSubjectSeries.id", criteria.getSeriesId()));
        }
        if (criteria.getLevelId() != null) {
            searchCriteria.add(Restrictions.eq("assignedSubjectLevel.id", criteria.getLevelId()));
        }
        if (criteria.getSubjectId() != null) {
            searchCriteria.add(Restrictions.eq("assignedSubject.id", criteria.getSubjectId()));
        }
        if (criteria.getCycleId() != null) {
            searchCriteria.add(Restrictions.eq("assignedSubjectCycle.id", criteria.getCycleId()));
        }
        return searchCriteria.list();
    }

    @Override
    public void deleteSubjectAssignment(SubjectAssignment subjectAssignment) {
        delete(subjectAssignment);
    }

}
